package com.xxx.meter.model.sp;

import android.content.Context;
import android.content.SharedPreferences;

import com.xxx.meter.ConfigClass;
import com.xxx.meter.app.App;


public class SharedPreferencesUtil {

    private static SharedPreferencesUtil sharedPreferencesUtils;

    private SharedPreferences sharedPreferences;

    private SharedPreferencesUtil() {
        sharedPreferences = App.getContext().getSharedPreferences(ConfigClass.SP_NAME, Context.MODE_PRIVATE);
    }

    public static SharedPreferencesUtil getInstance() {
        if (sharedPreferencesUtils == null) {
            synchronized (SharedPreferencesUtil.class) {
                if (sharedPreferencesUtils == null) {
                    sharedPreferencesUtils = new SharedPreferencesUtil();
                }
            }
        }
        return sharedPreferencesUtils;
    }


    /**
     * 保存
     */
    public void save(String tag, String content) {
        if (tag != null) {
            sharedPreferences.edit().putString(tag, content).apply();
        }
    }

    /**
     * 获取
     */
    public String get(String tag) {
        if (tag == null) {
            return null;
        }
        String string = sharedPreferences.getString(tag, null);
        if (string == null) {
            return null;
        }
        return string;
    }


    /**
     * 清除
     */
    public void cleanAll() {
        sharedPreferences.edit().clear().apply();
    }

}
